//
//  ZPComposePictureView.swift
//  Weibo
//
//  Created by 张鹏 on 16/7/3.
//  Copyright © 2016年 张鹏. All rights reserved.
//

import UIKit
// cell 可重用标识符
private let pictureViewCellId = "pictureViewCellId"

class ZPComposePictureView: UICollectionView {
    // 告知外界弹出相册
    var composePictureViewClosure:(()->())?
    
    // 保存image的数组
    var imageList:[UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 供外界调用的方法
    func addImages(image: UIImage){
        imageList.insert(image, atIndex: 0)
        // 如果count 大于9 删除最后一张图片
        if imageList.count > 9 {
            imageList.removeLast()
        }
        // 记得刷新数据
        reloadData()
    }
    
    
    // MARK: -  设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 注册cell
        registerClass(ZPComposePictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellId)
        dataSource = self
        delegate = self
        // 设置layout
        let layout =  collectionViewLayout as! UICollectionViewFlowLayout
        let itemMargin: CGFloat = 5
        let cellWH = (SCREENW - 20 - itemMargin*2)/3
        // 设置item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
    }
    /*
     如果有四张图片 显示 5个cell  最后一个加号图片
     如果有0张图片或者9张图片 没有加号按钮  显示9个cell
     */
    
}
extension ZPComposePictureView: UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imageList.count
        if count == 0 || count == 9{
            return count
        }else{
            return imageList.count + 1
        }
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellId, forIndexPath: indexPath) as! ZPComposePictureViewCell
        //[0.jpg,1.jpg,2.jpg]   显示4个cell .item == 3
        //        // 设置cell背景颜色
        
        
        //        cell.backgroundColor = RandomColor()
        // 给cell设置加号
        if imageList.count ==  indexPath.item {
            cell.image = nil
        }else{
            cell.image = imageList[indexPath.item]
        }
        
        
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        // 如果数组的count ==  indexPath.item 就是加号按钮
        if imageList.count ==  indexPath.item{
            print("加号按钮")
            composePictureViewClosure?()
        }
    }
    
}
// 自定义cell
class ZPComposePictureViewCell: UICollectionViewCell {
    // 供外界给其赋值
    var image: UIImage?{
        didSet{
            // bgImageView.image = image
            // 要设置加号按钮
            if image == nil{
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
            }else {
                // 正常显示
                bgImageView.image = image
                // 解决cell重用问题
                bgImageView.highlightedImage = nil
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 添加控件
        contentView.addSubview(bgImageView)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    }
    // MARK: -  懒加载控件
    private lazy var bgImageView: UIImageView = UIImageView()
}